package com.ihr360.payroll.controller.payslipconfig;

import cn.irenshi.entity.payslip.AppStaffPayslipDetailEntity;
import cn.irenshi.entity.payslip.AppStaffPayslipEntity;
import com.ihr360.commons.utils.RestToReturnUtils;
import com.ihr360.maven.plugin.ModuleCodeStore;
import com.ihr360.maven.plugin.annotation.FunctionDefinition;
import com.ihr360.maven.plugin.annotation.ModuleDefinition;
import com.ihr360.payroll.model.dto.salary.payslip.PayslipResult;
import com.ihr360.payroll.service.payslip.result.PayslipResultService;
import com.ihr360.rest.service.SimpleRestService;
import com.ihr360.rest.webmvc.AbstractRestSimpleWithLongIdController;
import com.ihr360.shared.advice.response.DisableResponseAdvice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author tomtao
 */
@RestController
@RequestMapping("/payslipResults")
@ModuleDefinition(mainModuleCode = ModuleCodeStore.Module.SALARY_CODE,
        mainModuleName = ModuleCodeStore.Module.SALARY_NAME,
        subModuleCode = ModuleCodeStore.Module.Salary.PAY_SLIP_CODE,
        subModuleName = ModuleCodeStore.Module.Salary.PAY_SLIP_NAME,
        hasExtendsAbstractRestController = true)
public class PayslipResultController extends AbstractRestSimpleWithLongIdController<PayslipResult> {

    @Autowired
    private PayslipResultService payslipResultService;

    @Override
    public SimpleRestService<PayslipResult, Long> getSimpleRestService() {
        return payslipResultService;
    }


    @FunctionDefinition(functionName = "获取工资单表头", functionCode = ModuleCodeStore.Function.PAYSLIP_RESULT)
    @GetMapping(value = "/getHeads/{payslipResultId}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map<String, String> getHeads(@PathVariable Long payslipResultId) {
        return payslipResultService.getPayslipHead(payslipResultId);
    }

    @FunctionDefinition(functionName = "获取工资单表头", functionCode = ModuleCodeStore.Function.EXPORT)
    @GetMapping(value = "/export/payslip/{payslipResultId}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void exportPayslip(@PathVariable Long payslipResultId, HttpServletResponse response) throws Exception {
        response.setContentType("application/vnd.ms-excel");
        String fileName = new String(("payslipList.xls").getBytes("UTF-8"), "iso-8859-1");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        payslipResultService.exportPayslip(payslipResultId, response.getOutputStream());
    }

    @FunctionDefinition(functionName = "发布工资单", functionCode = ModuleCodeStore.Function.PAYSLIP_RESULT)
    @GetMapping(value = "/publishPayslip/{payslipResultId}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void publishPayslip(@PathVariable Long payslipResultId) {
        payslipResultService.publishPayslip(payslipResultId);
        RestToReturnUtils.setMessage("发布成功!");
    }

    @FunctionDefinition(functionName = "撤销工资单", functionCode = ModuleCodeStore.Function.PAYSLIP_RESULT)
    @GetMapping(value = "/cancelPublishPayslip/{payslipResultId}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void cancelPublishPayslip(@PathVariable Long payslipResultId) {
        payslipResultService.cancelPublishPayslip(payslipResultId);
        RestToReturnUtils.setMessage("撤销成功!");
    }

    @FunctionDefinition(functionName = "修改工资单配置", functionCode = ModuleCodeStore.Function.PAYSLIP_RESULT)
    @GetMapping(value = "/changePayslipConfig/{payslipResultId}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void changePayslipConfig(@PathVariable Long payslipResultId, @RequestParam Long payslipConfigId) {
        payslipResultService.changePayslipConfig(payslipResultId, payslipConfigId);
        RestToReturnUtils.setMessage("修改成功!");
    }

    @FunctionDefinition(functionName = "App端获取员工工资单列表", isInternal = true, functionCode = ModuleCodeStore.Function.PAYSLIP_RESULT)
    @GetMapping(value = "/getStaffPayslipList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @DisableResponseAdvice
    public List<AppStaffPayslipEntity> getStaffPayslipList(@RequestParam String companyId, @RequestParam String staffId, @RequestParam int orderBy) {
        return payslipResultService.getStaffPayslipList(companyId, staffId, orderBy);
    }

    @FunctionDefinition(functionName = "App端获取一个工资单详情", isInternal = true, functionCode = ModuleCodeStore.Function.PAYSLIP_RESULT)
    @GetMapping(value = "/getStaffPayslipDetail", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @DisableResponseAdvice
    public AppStaffPayslipDetailEntity getStaffPayslipDetail(@RequestParam Long id) {
        return payslipResultService.getStaffPayslipDetail(id);
    }

}
